package com.ruicar.afs.cloud.apply.manage.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConfig;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.image.condition.RevolveCondition;
import com.ruicar.afs.cloud.image.condition.UploadCondition;
import com.ruicar.afs.cloud.image.entity.ComAttachmentManagement;
import com.ruicar.afs.cloud.manage.outer.api.common.feign.ApplyFileFeign;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 贷后多品合同系统，附件操作
 */
@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("/contractManageUpload")
public class LoanContractFileController {

    private final ApplyFileFeign applyFileFeign;
    private final ApplyConfig applyConfig;

    @ApiModelProperty("保存附件信息")
    @PostMapping("/saveAttachmentFile")
    public IResponse saveAttachmentFile(@RequestParam("attachmentId") String attachmentId,
                                                                        @RequestParam("attachmentName") String attachmentName,
                                                                        @RequestParam("fileType") String fileType,
                                                                        @RequestParam("fileName") String fileName,
                                                                        @RequestParam("fileMd5") String fileMd5,
                                                                        @RequestParam("applyNo") String busiNo,
                                                                        @RequestParam("belongNo") String belongNo) {
        IResponse response = applyFileFeign.saveAttachmentFile(attachmentId, attachmentName, fileType, fileName, fileMd5, busiNo, belongNo, head());
        return response;
    }

    @ApiModelProperty("删除附件信息")
    @PostMapping("/deleteFile")
    public IResponse deleteFile(@RequestBody JSONObject fileList) {
        IResponse response = applyFileFeign.deleteFile(fileList, head());
        return response;
    }

    @ApiModelProperty("更新附件信息")
    @PostMapping("/updateFile")
    public IResponse updateFile(@RequestBody JSONObject fileParam) {
        IResponse response = applyFileFeign.updateFile(fileParam, head());
        return response;
    }

    @PostMapping(value = "/revolveFile")
    @ApiOperation(value = "旋转保存图片")
    public IResponse revolveFile(@RequestBody RevolveCondition revolveCondition){

        IResponse response = applyFileFeign.revolveFile(JSON.toJSONString(revolveCondition), head());
        return response;
    }

        @PostMapping(value = "/getUploadListByGroup")
    @ApiOperation(value = "根据业务场景和前端参数获取影像件类型")
    public IResponse getUploadListByGroup(@RequestBody UploadCondition uploadCondition) {
        IResponse response = applyFileFeign.getUploadListByGroup(JSON.toJSONString(uploadCondition), head());
        return response;
    }

    @PostMapping(value = "/checkFile")
    @ApiOperation(value = "根据前端参数检查文件是否已上传完整")
    public IResponse checkFile(@RequestBody UploadCondition uploadCondition) {
        IResponse response = applyFileFeign.checkFile(JSON.toJSONString(uploadCondition), head());
        return response;
    }

    private Map head(){
        Map<String, String> headers = new HashMap<>();
        headers.put("clientId",applyConfig.getContractClientId());
        headers.put("clientSecret",applyConfig.getContractClientSecret());
        return headers;
    }
}
